package com.sword.onlysword;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * com.sword.onlysword.PersonProxy
 * Created by SwordLiu on 2018-03-16.
 */
public class PersonProxy implements InvocationHandler {

    volatile Person person;

    public Person getInstance(Person person) {
        this.person = person;
        Class<?> clazz = person.getClass();
        Object obj = Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), this);
        return (Person) obj;
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("代理开始");
        method.invoke(person, args);
        System.out.println("代理结束");
        return null;
    }
}
